package com.dd.cloud.common.config.redis;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

/**
 * @Description: redisson分布式锁配置
 * @Author: czg
 * @Date: 2023/4/14 15:16
 * @Version: 1.0.0
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "spring.redis")
//@PropertySource("classpath:application.yml")
public class MyRedissonConfig {
    private String host;
    private String port;
    private String password;
    private int database;

    @Bean
    public RedissonClient getRedisson(){
        Config config = new Config();
        config.useSingleServer().setAddress("redis://" + host + ":" + port).setDatabase(database);
        if(StringUtils.isNotBlank(password)){
            config.useSingleServer().setPassword(password);
        }
        return Redisson.create(config);
    }
}
